package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

    @Autowired
    private BaseAttributeValueMapper baseAttributeValueMapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;
    /**
     * 查询所有的分类数据
     * @param
     * @return null
     */
    @Override
    public List<JSONObject> getBaseCategoryList() {
        // 查询所有的分类数据 包含一二三级分类Id
        List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
        // 根据一级分类Id 将数据分桶
        Map<Long, List<BaseCategoryView>> category1Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        // 遍历每个一级分类 获取一级分类下的二级分类和三级信息
        List<JSONObject> list = new ArrayList<>();
        category1Map.entrySet().stream().forEach(category1Entry ->{
            // 获取一级分类的Id
            Long category1Id = category1Entry.getKey();
            // 获取一级分类Id 对应的集合数据
            List<BaseCategoryView> category1ViewList = category1Entry.getValue();
            // 将一级分类Id下对应的数据 根据二级Id分桶
            Map<Long, List<BaseCategoryView>> category2Map = category1ViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            // 遍历每一个二级数据 获取二级数据下的三级数据
            List<JSONObject> category1Child = new ArrayList<>();
            category2Map.entrySet().forEach(category2Entry ->{
                // 获取一级分类的Id
                Long category2Id = category2Entry.getKey();
                // 获取一级分类Id 对应的集合数据
                List<BaseCategoryView> category2ViewList = category2Entry.getValue();
                // 将二级分类Id下对应的数据 根据三级分类Id分桶
                Map<Long, List<BaseCategoryView>> category3Map = category2ViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory3Id));
                // 遍历每一个三级数据 封装数据项
                List<JSONObject> category2Child = category3Map.entrySet().stream().map(category3Entry -> {
                    // 获取一级分类的Id
                    Long category3Id = category3Entry.getKey();
                    // 获取一级分类Id 对应的集合数据
                    List<BaseCategoryView> category3ViewList = category3Entry.getValue();
                    JSONObject category3 = new JSONObject();
                    category3.put("categoryId", category3Id);
                    category3.put("categoryName", category3ViewList.get(0).getCategory3Name());
                    return category3;
                }).toList();
                // 封装每个二级数据项
                JSONObject category2 = new JSONObject();
                category2.put("categoryId", category2Id);
                category2.put("categoryName", category2ViewList.get(0).getCategory2Name());
                category2.put("categoryChild", category2Child);
                // 将每个封装好的二级数据项添加到一级数据项的子节点中
                category1Child.add(category2);
            });
            // 封装一级数据项
            JSONObject category1 = new JSONObject();
            category1.put("categoryId", category1Id);
            category1.put("categoryName", category1ViewList.get(0).getCategory1Name());
            category1.put("categoryChild", category1Child);
            // 将每个一级节点添加到要返回的集合中
            list.add(category1);
        });

        return list;
    }

    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        // 先根据以及分类Id 查询标签集合
        List<BaseAttribute> baseAttributeList = baseAttributeMapper.selectList(new LambdaQueryWrapper<BaseAttribute>().eq(BaseAttribute::getCategory1Id, category1Id));
        // 遍历标签集合
        List<BaseAttribute> list = baseAttributeList.stream().map(baseAttribute -> {
            // 获取标签Id
            Long attributeId = baseAttribute.getId();
            // 根据标签Id 查询标签值
            List<BaseAttributeValue> baseAttributeValueList = baseAttributeValueMapper.selectList(new LambdaQueryWrapper<BaseAttributeValue>().eq(BaseAttributeValue::getAttributeId, attributeId));
            // 封装标签值集合到标签项中
            baseAttribute.setAttributeValueList(baseAttributeValueList);

            return baseAttribute;
        }).toList();
        return list;
        // return baseAttributeMapper.selectBaseAttributeList(category1Id);
    }

    /**
     * 根据三级分类查询一级二级三级分类的视图
     * @param category3Id
     * @return
     */
    @Override
    public BaseCategoryView getBaseCategoryView(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);
    }

    /**
     * 1106 查询指定一级分类下前7个三级分类数据
     * @param category1Id
     * @return
     */
    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        return baseCategory3Mapper.findTopBaseCategory3(category1Id);
    }

    /**
     * 1106 查询指定一级分类下全部的三级分类数据
     * @param category1Id
     * @return
     */
    @Override
    public JSONObject getAllBaseCategoryListById(Long category1Id) {
        // 根据一级分类id 查询所有的baseCategoryView
        List<BaseCategoryView> baseCategoryViewList =
                baseCategoryViewMapper.selectList(new LambdaQueryWrapper<>(BaseCategoryView.class)
                                                          .eq(BaseCategoryView::getCategory1Id, category1Id));
        // 根据二级分类id 将所有视图分桶
        Map<Long, List<BaseCategoryView>> category2Map =
                baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
        // 遍历每个二级分类entrySet
        List<JSONObject> category1Child = category2Map.entrySet().stream().map(category2Entry -> {
            JSONObject category2 = new JSONObject();
            category2.put("categoryId", category2Entry.getKey());
            category2.put("categoryName", category2Entry.getValue().get(0).getCategory2Name());

            List<BaseCategoryView> category2ViewList = category2Entry.getValue();
            // 遍历二级分类列表 封装每个三级分类数据项
            List<JSONObject> category2Child = category2ViewList.stream().map(category3View -> {
                JSONObject category3 = new JSONObject();
                category3.put("categoryId", category3View.getCategory3Id());
                category3.put("categoryName", category3View.getCategory3Name());
                return category3;
            }).toList();
            // 封装 category2 的子节点数据
            category2.put("categoryChild", category2Child);
            return category2;
        }).toList();
        JSONObject category1 = new JSONObject();
        category1.put("categoryId", category1Id);
        category1.put("categoryName", baseCategoryViewList.get(0).getCategory1Name());
        category1.put("categoryChild", category1Child);
        return category1;
    }
}
